package relaychat.websocket;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

/**
 * WebSocketChatServlet
 * 
 * This class manages the HTTP Upgrade to WebSocket 
 * and migration from a HTTP Connection to a WebSocket Connection
 * 
 */
public class WebSocketChatServlet extends WebSocketServlet {

	
	private static final long serialVersionUID = 5610501345675935366L;
	public final Set<ChatWebSocket> users = new CopyOnWriteArraySet<ChatWebSocket>();

	/**
	 * doGet
	 * params request, response
	 * throws IO and servlet exception
	 * forwards the request and response
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getServletContext().getNamedDispatcher("default").forward(request,
				response);
	}

	/**
	 * doWebSocketConnect
	 * params arg0 arg1
	 * returns WebSocket class to the server
	 */
	@Override
	public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
		return new ChatWebSocket(users);
	}

}
